#ifndef TARGET_SENDER_H_
#define TARGET_SENDER_H_
#include <dirent.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/time.h>

#include <iostream>
#include <string>
#include <vector>
#include <thread>
#include <algorithm>  
#include <regex>
#include <tuple>

#include "thread_safe_queue.h"
#include "utils.h"
#include "dvpp_process.h"

// 自己定制要发送的数据类型
typedef struct SendData {
    vector<BBox> rlts;
    int image_id;
    cv::Mat bgr;
} SendData;

class TargetSender {
public:
    explicit TargetSender();

    ~TargetSender();

    void DestroyResource();

    Result Init(aclrtContext& context, aclrtStream& stream, std::string networkAddr);

    Result SendDataEnQueue(vector<BBox>& resultData, int image_id, cv::Mat& bgr);   // 发图
    Result SendDataEnQueue(vector<BBox>& resultData, int image_id);                  // 只发结果

    int GetQueueSize() { return SendDataQueue_.size(); }

private:
    static void TargetSendThreadFunction(void* senderSelf); 
    SendData SendDataOutQueue();
    
    void Send(SendData& data);
    void SendResult(vector<BBox>& rlts, int image_id);
    void SendJpeg(const cv::Mat& bgr, int image_id);
    void SendImage(cv::Mat& bgr, int image_id);
    Result SendPackage(unsigned char data[], int size);
    AtlasError SetAclContext();

    ThreadSafeQueue<SendData> SendDataQueue_;

    int clifd;
    struct sockaddr_in client_addr;
    socklen_t nRecLen;

    aclrtContext context_;
    aclrtStream stream_;
    DvppProcess dvpp_;

};

#endif /* TARGET_SENDER_H_ */
